package com.monicash.monicash.main.fragment

import android.os.Bundle
import android.os.Handler
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import com.monicash.monicash.BafBase.BAFBaseFragment
import com.monicash.monicash.R
import com.monicash.monicash.main.MainActivity
import com.monicash.monicash.main.adapter.HomeImageBannerAdapter
import com.monicash.monicash.widge.HomeGuideDialog
import com.youth.banner.Banner

/**
 * 首页
 * author: Created by 闹闹 on 2019/11/13
 * version: 1.0.0
 */
class HomeFragment : BAFBaseFragment<MainActivity, HomeFragment>() {

    companion object{
        // 未登录
        const val STATUS_NOT_LOGIN = 100
        // 审核中
        const val STATUS_REVIEWING = 101
        // 审核被拒
        const val STATUS_REVIEWING_REJECT = 102
        // 放贷中
        const val STATUS_LOAN = 103
        // 还款中
        const val STATUS_REPAYMENT = 104
        // 已逾期
        const val STATUS_OVERDUE = 105
        // 已登录
        const val STATUS_LOGIN = 106
    }

    private val llCardStatusNotLoan by lazy { mView!!.findViewById<LinearLayoutCompat>(R.id.ll_card_status_not_loan) }
    private val llCardStatusLoan by lazy { mView!!.findViewById<LinearLayoutCompat>(R.id.ll_card_status_loan) }
    private val tvRealizandoLa by lazy { mView!!.findViewById<TextView>(R.id.tv_realizando_la) }
    private val tvPagarYa by lazy { mView!!.findViewById<TextView>(R.id.tv_pagar_ya) }
    private val llRevisando by lazy { mView!!.findViewById<LinearLayoutCompat>(R.id.ll_revisando) }
    private val llIntentaloDespues by lazy { mView!!.findViewById<LinearLayoutCompat>(R.id.ll_intentalo_despues) }
    private val tvAplicarYa by lazy { mView!!.findViewById<TextView>(R.id.tv_aplicar_ya) }
    private val tvSoliciteloYa by lazy { mView!!.findViewById<TextView>(R.id.tv_solicitelo_ya) }
    private val tvMoneyDesc by lazy { mView!!.findViewById<TextView>(R.id.tv_money_desc) }
    private val tvDurationDesc by lazy { mView!!.findViewById<TextView>(R.id.tv_duration_desc) }
    private val flQuejas by lazy { mView!!.findViewById<FrameLayout>(R.id.fl_quejas) }

    fun newInstance(index: Int): HomeFragment {
        val fragment = HomeFragment()
        val bundle = Bundle()
        bundle.putInt(INDEX, index)
        fragment.arguments = bundle
        return fragment
    }

    override fun initLayout() = R.layout.fragment_home

    override fun afterInjectView(savedInstanceState: Bundle?) {
        initEvent()
        (mView?.findViewById(R.id.bn_home) as? Banner<String,HomeImageBannerAdapter>)?.apply {
            setAdapter(HomeImageBannerAdapter(arrayListOf("1","2","3","4")),true)
            setBannerGalleryEffect(100, 16)
        }
//        setCurStatus(STATUS_NOT_LOGIN)
//        setCurStatus(STATUS_REVIEWING)
//        setCurStatus(STATUS_REVIEWING_REJECT)
//        setCurStatus(STATUS_LOAN)
//        setCurStatus(STATUS_REPAYMENT)
//        setCurStatus(STATUS_OVERDUE)
        setCurStatus(STATUS_LOGIN)
        context?.let {
            Handler().postDelayed(Runnable {
                HomeGuideDialog(it,tvSoliciteloYa).show()
            },2000L)
        }

    }

    private fun setCurStatus(pageStatus: Int) {
        llCardStatusNotLoan.visibility = View.GONE
        llCardStatusLoan.visibility = View.GONE
        tvRealizandoLa.visibility = View.GONE
        tvPagarYa.visibility = View.GONE
        llRevisando.visibility = View.GONE
        llIntentaloDespues.visibility = View.GONE
        tvAplicarYa.visibility = View.GONE
        tvSoliciteloYa.visibility = View.GONE
        flQuejas.visibility = View.GONE
        when (pageStatus) {
            STATUS_LOGIN -> {
                llCardStatusNotLoan.visibility = View.VISIBLE
                tvSoliciteloYa.visibility = View.VISIBLE
                tvMoneyDesc.apply {
                    setText("Obtenga un préstamo en 3 minutos")
                    setTextColor(resources.getColor(R.color.black))
                }
            }
            STATUS_NOT_LOGIN -> {
                llCardStatusNotLoan.visibility = View.VISIBLE
                tvAplicarYa.visibility = View.VISIBLE
                tvMoneyDesc.apply {
                    setText("Obtenga un préstamo en 3 minutos")
                    setTextColor(resources.getColor(R.color.black))
                }
            }
            STATUS_REVIEWING -> {
                llCardStatusLoan.visibility = View.VISIBLE
                llRevisando.visibility = View.VISIBLE
                tvMoneyDesc.apply {
                    setText("Obtenga un préstamo en 3 minutos")
                    setTextColor(resources.getColor(R.color.black))
                }
                tvDurationDesc.setTextColor(resources.getColor(R.color.gray_666666))
            }
            STATUS_REVIEWING_REJECT -> {
                llCardStatusLoan.visibility = View.VISIBLE
                llIntentaloDespues.visibility = View.VISIBLE
                tvMoneyDesc.apply {
                    setText("El siguiente de aplica en  09-12-2023")
                    setTextColor(resources.getColor(R.color.gray_666666))
                }
                tvDurationDesc.setTextColor(resources.getColor(R.color.gray_666666))
            }
            STATUS_LOAN -> {
                llCardStatusLoan.visibility = View.VISIBLE
                tvRealizandoLa.visibility = View.VISIBLE
                tvMoneyDesc.apply {
                    setText("")
                }
                tvDurationDesc.setTextColor(resources.getColor(R.color.gray_666666))
            }
            STATUS_REPAYMENT -> {
                llCardStatusLoan.visibility = View.VISIBLE
                tvPagarYa.visibility = View.VISIBLE
                tvMoneyDesc.apply {
                    setText("Pagar a tiempo tiene el potencial de aumentar")
                    setTextColor(resources.getColor(R.color.gray_666666))
                }
                tvDurationDesc.setTextColor(resources.getColor(R.color.gray_666666))
            }
            STATUS_OVERDUE -> {
                llCardStatusLoan.visibility = View.VISIBLE
                tvPagarYa.visibility = View.VISIBLE
                tvMoneyDesc.apply {
                    val ss3 = SpannableString("La tarifa de penalización es 8%/día")
                    ss3.setSpan(ForegroundColorSpan(resources.getColor(R.color.text_red_color)), ss3.indexOf("8%"),
                        ss3.indexOf("8%") + 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                    setText(ss3)
                    setTextColor(resources.getColor(R.color.gray_666666))
                }
                tvDurationDesc.setTextColor(resources.getColor(R.color.text_red_color))
                flQuejas.visibility = View.VISIBLE
            }
        }
    }

    private fun initEvent() {

    }

    override fun lazyLoad() {}

    override fun onDestroy() {
        super.onDestroy()
        unregisterEventBus(this)
    }


}